﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataFrameworkAccess;

namespace Xinkui.Common.FileAction {
    public class FileDataAccess {
        public static int CreateFile(FileEntity fEntity) {
            DataCommand command = DataCommandManager.GetDataCommand("File.CreateAttachment");

            command.SetParameterValue("@ID", fEntity.FileID);
            command.SetParameterValue("@SourceType", (int)fEntity.SourceType);
            command.SetParameterValue("@SourceID", fEntity.SourceID);
            command.SetParameterValue("@UserID", fEntity.UserID);
            command.SetParameterValue("@FileName", fEntity.FileName.ToLower());
            command.SetParameterValue("@FileExtension", fEntity.FileExtension.ToLower());
            command.SetParameterValue("@FileSize", fEntity.FileSize);
            command.SetParameterValue("@FilePath", fEntity.FilePath.ToLower());
            command.SetParameterValue("@CreateDate", fEntity.UpdateDate);
            command.SetParameterValue("@IsDeleted", 0);
            command.SetParameterValue("@ServerPath", fEntity.ServerPath.ToLower());

            command.ExecuteNonQuery();

            return fEntity.FileID;
        }

        public static int GetNewFileID() {
            DataCommand command = DataCommandManager.GetDataCommand("Common.BuildIdentity");

            command.SetParameterValue("@TableName", "CommonAttachment");

            return (int)command.ExecuteScalar();
        }

        public static FileEntity GetFileEntityByID(int attachID) {
            DataCommand command = DataCommandManager.GetDataCommand("File.GetFileByID");

            command.SetParameterValue("@ID", attachID);

            return command.ExecuteEntity<FileEntity>();
        }


        public static FileEntity GetFileEntityByType(int sourceID, int sourceType) {
            DataCommand command = DataCommandManager.GetDataCommand("File.GetFileByType");

            command.SetParameterValue("@SourceType", sourceType);
            command.SetParameterValue("@SourceID", sourceID);

            return command.ExecuteEntity<FileEntity>();
        }


        public static List<FileEntity> GetFileListByType(int sourceID, int sourceType) {
            DataCommand command = DataCommandManager.GetDataCommand("File.GetFileByType");

            command.SetParameterValue("@SourceType", sourceType);
            command.SetParameterValue("@SourceID", sourceID);

            return command.ExecuteEntityList<FileEntity>();
        }

        public static int UpdateAttachment(FileEntity fEntity) {
            DataCommand command = DataCommandManager.GetDataCommand("File.UpdateFile");

            command.SetParameterValue("@ID", fEntity.FileID);
            command.SetParameterValue("@SourceType", fEntity.SourceType);
            command.SetParameterValue("@SourceID", fEntity.SourceID);
            command.SetParameterValue("@UserID", fEntity.UserID);
            command.SetParameterValue("@FileName", fEntity.FileName.ToLower());
            command.SetParameterValue("@FileExtension", fEntity.FileExtension.ToLower());
            command.SetParameterValue("@FileSize", fEntity.FileSize);
            command.SetParameterValue("@FilePath", fEntity.FilePath.ToLower());
            command.SetParameterValue("@CreateDate", fEntity.UpdateDate);
            command.SetParameterValue("@IsDeleted", 0);
            command.SetParameterValue("@ServerPath", fEntity.ServerPath.ToLower());

            return command.ExecuteNonQuery();
        }

        public static int DeleteFileByType(int attachID, int sourceID, int sourceType) {
            DataCommand command = DataCommandManager.GetDataCommand("File.DeleteFileByType");

            command.SetParameterValue("@ID", attachID);
            command.SetParameterValue("@SourceType", sourceType);
            command.SetParameterValue("@SourceID", sourceID);

            return command.ExecuteNonQuery();
        }

        public static int DeleteFileByID(int attachID) {
            DataCommand command = DataCommandManager.GetDataCommand("File.DeleteFileByID");

            command.SetParameterValue("@ID", attachID);

            return command.ExecuteNonQuery();
        }
    }
}
